package com.project.m.api.busi.handlers.hicatcard.Inner;

import com.project.m.api.busi.bean.inner.InnerUserRegisterReq;
import com.project.m.api.busi.bean.inner.InnerUserRegisterResp;
import com.project.m.api.busi.config.BusiError;
import com.project.m.api.busi.db.entity.hicatcard.card.TCard;
import com.project.m.api.busi.db.entity.hicatcard.user.TUser;
import com.project.m.api.busi.db.entity.hicatcard.user.TUserCardRechargeNewUser;
import com.project.m.api.busi.db.service.hicatcard.card.CardService;
import com.project.m.api.busi.db.service.hicatcard.user.UserCardRechargeNewUserService;
import com.project.m.api.busi.db.service.hicatcard.user.UserService;
import com.project.m.api.busi.handlers.MerchantBizParamChecker;
import com.project.m.api.validates.ValidateParam;
import com.project.m.api.validates.ValidateRet;
import com.project.m.api.validates.ValidateType;
import com.project.m.api.validates.ValidateUtil;
import com.project.m.api.busi.util.TimeUtil;
import com.project.m.api.common.biz.BizHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Component
public class InnerUserRegisterBizHandler extends MerchantBizParamChecker<InnerUserRegisterReq, InnerUserRegisterResp>
        implements BizHandler<InnerUserRegisterReq, InnerUserRegisterResp> {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    UserService userService;

    @Autowired
    CardService cardService;

    @Autowired
    UserCardRechargeNewUserService userCardRechargeNewUserService;

    @Override
    public InnerUserRegisterResp handle(InnerUserRegisterReq bizRequest) throws Exception {
        InnerUserRegisterResp bizResp = new InnerUserRegisterResp();
        try {
            ValidateRet vRet = checkParam(bizRequest, bizResp);
            if (!vRet.isValid()) {
                return bizResp;
            }


            List<TUser> userList = userService.findByPhoneAndEnable(bizRequest.getPhone(), true);
            if (!CollectionUtils.isEmpty(userList)) {
                bizResp.setNewUser(Boolean.TRUE);
                bizResp.setResult_code(BusiError.ERR_CODE_SUCCESS);
                bizResp.setError_message("");
                return bizResp;
            } else {

                TCard card = cardService.findByCardId(bizRequest.getCard_id());
                if (card == null) {
                    bizResp.setResult_code(BusiError.ERR_CODE_CARD_NOT_EXIST);
                    bizResp.setError_message(BusiError.ERR_MSG_CARD_NOT_EXIST);
                    return bizResp;
                }
                //用phone 找用户，有返回成功
                //失败，在
                TUserCardRechargeNewUser userCardRechargeNewUser = new TUserCardRechargeNewUser();
                userCardRechargeNewUser.setPhone(bizRequest.getPhone());
                userCardRechargeNewUser.setCardId(bizRequest.getCard_id());
                userCardRechargeNewUser.setNumber(bizRequest.getNumber());
                userCardRechargeNewUser.setStatus(TUserCardRechargeNewUser.STATUS_NOT_GET);
                userCardRechargeNewUser.setCreateTime(TimeUtil.getCurrTime());
                userCardRechargeNewUser.setUpdateTime(TimeUtil.getCurrTime());
                userCardRechargeNewUser.setEnable(true);

                userCardRechargeNewUserService.saveUserCardRechargeNewUser(userCardRechargeNewUser);

                bizResp.setNewUser(Boolean.FALSE);
                bizResp.setResult_code(BusiError.ERR_CODE_SUCCESS);
                bizResp.setError_message("");
                return bizResp;
            }


        } catch (Exception e) {
            logger.error("InnerUserRegisterBizHandler", e);
            bizResp.setResult_code(BusiError.ERR_CODE_DB_SAVE);
            bizResp.setError_message(BusiError.ERR_MSG_DB_SAVE);
            return bizResp;
        }
    }

    @Override
    public ValidateRet checkParam(InnerUserRegisterReq req, InnerUserRegisterResp resp) {
        ValidateRet vRet = super.checkParam(req, resp);
        if (!vRet.isValid())
            return vRet;
        List<ValidateParam> paramList = new ArrayList<ValidateParam>();
        paramList.add(new ValidateParam("phone", ValidateType.NOTBLANK, req.getPhone()));

        vRet = ValidateUtil.validate(paramList);
        if (vRet == null) {
            vRet = new ValidateRet(true, "");
        }

        if (!vRet.isValid()) {
            resp.setResult_code(BusiError.ERR_CODE_PARAM_BAD_REQ);
            resp.setError_message(vRet.getErrMsg());
        }

        return vRet;
    }
}
